package com.liuhao.ssm.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.liuhao.ssm.annotation.AuthPassport;
import com.liuhao.ssm.controller.BaseController;
import com.liuhao.ssm.domain.UserInfo;
import com.liuhao.ssm.util.ResultData;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by liuHao on 2017/5/25.
 */
public class ControllerInterceptor implements HandlerInterceptor {

    // Controller方法调用之前
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
            HandlerMethod h = (HandlerMethod) handler;
            AuthPassport authPassport = h.getMethodAnnotation(AuthPassport.class);
            if (h.getBean() instanceof BaseController) {
                // 登录验证
                if (authPassport != null && authPassport.value() == false) {
                    //如果不需要登录就需要访问到方法，则需要方法前面加	@AuthPassport(value = false)
                    return true;
                } else {
                    // 拦截代码，通过验证则返回true，否则返回false
                    String[] status = getLoginStatus(request);
                    if (status[0].equals("Normal")) {
                        return true;
                    } else {
                        rendNoLogin(response);
                        return false;
                    }
                }
            } else {
                rendNoLogin(response);
                return false;
            }
        } else {
            return true;
        }
    }

    private void rendNoLogin(HttpServletResponse response) throws Exception {
        ResultData data = ResultData.error(-1, "没有登录");
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(JSONObject.toJSONString(data));
    }

    // Controller方法调用之后，页面渲染之前
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
                           Object arg2, ModelAndView arg3) throws Exception {

    }

    // Controller方法调用之后，页面渲染之后
    public void afterCompletion(HttpServletRequest arg0,
                                HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {

    }

    public String[] getLoginStatus(HttpServletRequest request) {

        String[] result = new String[]{"Normal", "Normal"};
        HttpSession session = request.getSession();
        UserInfo user = (UserInfo) session.getAttribute("LoginInfo");
        if (user == null) {
            //获取不到登录用户	超时
            result[0] = "timeout";
            result[1] = "timeoutuser";
        }
        return result;
    }
}
